//
//  MenuViewController.m
//  BobNews
//
//  Created by lanou3g on 16/5/5.
//  Copyright © 2016年 lanou3g. All rights reserved.
//

#import "MenuViewController.h"
#define KLeftWidth [UIScreen mainScreen].bounds.size.width * 0.7

@interface MenuViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong) UITableView *leftTableView;

@end

@implementation MenuViewController

-(BOOL)isShow{
    if (self.centerViewController.view.transform.tx > 0) {
        return YES;
    }
    return NO;
}
static MenuViewController *menuVC = nil;
+(instancetype)instancetypeWithMenuLeftViewController:(UIViewController *)leftVC CenterViewController:(UIViewController *)centerVC{
    if (!menuVC) {
        menuVC = [[MenuViewController alloc]init];
        [UIApplication sharedApplication].keyWindow.rootViewController = menuVC;
        menuVC.leftViewController = leftVC;
        menuVC.centerViewController = centerVC;
        if (leftVC) {
            [menuVC addChildViewController:leftVC];
            [menuVC.view addSubview:leftVC.view];
            
        }else{
            [menuVC setupShowMenu];
        }
        if (centerVC) {
            [menuVC addChildViewController:centerVC];
            [menuVC.view addSubview:centerVC.view];
        }
        
    }
    return menuVC;
}

-(void)setupShowMenu{
    
    self.leftTableView = [[UITableView alloc]initWithFrame:CGRectMake(-KLeftWidth, 0, KLeftWidth, [UIScreen mainScreen].bounds.size.height) style:UITableViewStylePlain];
    self.leftTableView.delegate = self;
    self.leftTableView.dataSource = self;
    [self.view addSubview:self.leftTableView];
    
    UIImageView *backImgView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"back"]];
    backImgView.frame = CGRectMake(0, 0, KLeftWidth, self.view.frame.size.height);
    self.leftTableView.backgroundView = backImgView;
    self.leftTableView.separatorStyle = UITableViewCellSeparatorStyleNone;


}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.menuArray.count;
}

-(void)setMenuArray:(NSMutableArray *)menuArray{
    if (_menuArray != menuArray) {
        _menuArray = nil;
        _menuArray = menuArray;
        [self.leftTableView reloadData];
    }
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
     static NSString *identifier = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    cell.textLabel.text = self.menuArray[indexPath.row];
    cell.backgroundColor = [UIColor clearColor];

    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if ([_MenuDelegate respondsToSelector:@selector(didClickMenuIndex:Title:)]) {
        [_MenuDelegate didClickMenuIndex:indexPath.row Title:self.menuArray[indexPath.row]];
    }
    [self closeMenu];
}

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    return @"菜单栏项目";
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 63;
}


+(instancetype)getMenu{
    return menuVC;
}

-(void)openMenu{
    [self openMenuDuration:0.7];
}

-(void)openMenuDuration:(CGFloat)duration{
    [UIView animateWithDuration:duration animations:^{
        self.centerViewController.view.transform = CGAffineTransformMakeTranslation(KLeftWidth, 0);
        self.leftViewController.view.transform = CGAffineTransformMakeTranslation(KLeftWidth, 0);
        self.leftTableView.transform = CGAffineTransformMakeTranslation(KLeftWidth, 0);
        
    }];
}

-(void)closeMenu{
    [self closeMenuDuration:0.7];
}

-(void)closeMenuDuration:(CGFloat)duration{
    [UIView animateWithDuration:duration animations:^{
        self.centerViewController.view.transform = CGAffineTransformMakeTranslation(0, 0);
        self.leftViewController.view.transform = CGAffineTransformMakeTranslation(0, 0);
        self.leftTableView.transform = CGAffineTransformMakeTranslation(0, 0);
        
    }];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panMenuWithPanGesture:)];
    [self.view addGestureRecognizer:pan];
    
}


static CGPoint begin;
static CGAffineTransform transform;
- (void)panMenuWithPanGesture:(UIPanGestureRecognizer *)pan {
    
    CGPoint translatePoint = CGPointZero;
    if (pan.state == UIGestureRecognizerStateBegan) {
        begin = [pan locationInView:self.view];
        transform = self.centerViewController.view.transform;
    }
    if (pan.state == UIGestureRecognizerStateChanged) {
        CGPoint end = [pan locationInView:self.view];
        translatePoint = CGPointMake(end.x - begin.x, 0);
    }
    if (pan.state == UIGestureRecognizerStateCancelled || pan.state == UIGestureRecognizerStateEnded) {
        if ([pan velocityInView:self.view].x > 0) {
            [self openMenuDuration:0.13];
        }else {
            [self closeMenuDuration:0.13];
        }
        return;
    }
    if(self.centerViewController.view.transform.tx >= 0 && self.centerViewController.view.transform.tx <= KLeftWidth) {
        self.centerViewController.view.transform = CGAffineTransformTranslate(transform, translatePoint.x, 0);
        self.leftViewController.view.transform = CGAffineTransformTranslate(transform,translatePoint.x, 0);
        self.leftTableView.transform = CGAffineTransformTranslate(transform, translatePoint.x, 0);
    }
}







- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
